package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbBrandMapper;
import com.aisile.pojo.TbBrand;
import com.aisile.pojo.TbBrandExample;
import com.aisile.sellergoods.service.BrandService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @标题 BrandServiceImpl
 * @作者 Mr.chen
 * @创建日期 2018/12/26 15:11
 * @版本号 1.0
 **/
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    TbBrandMapper tbBrandMapper;

    @Override
    public List<TbBrand> findAll() {
        return tbBrandMapper.selectByExample(null);
    }

    @Override
    public PageResult findPage(int page, int rows,TbBrand tbBrand) {
        PageHelper.startPage(page,rows);
        String name = tbBrand.getName();
        String firstChar = tbBrand.getFirstChar();
        TbBrandExample tbBrandExample = new TbBrandExample();

        if (name!=null && !"".equals(name)){
            tbBrandExample.createCriteria().andNameLike("%"+tbBrand.getName()+"%");
        }
        if (firstChar!=null && !"".equals(firstChar)){
            tbBrandExample.createCriteria().andFirstCharLike("%"+tbBrand.getFirstChar()+"%");
        }
        Page<TbBrand> pageResult =  (Page<TbBrand>)tbBrandMapper.selectByExample(tbBrandExample);
        return new PageResult(pageResult.getTotal(),pageResult.getResult());
    }

    @Override
    public void save(TbBrand tbBrand) {
        tbBrandMapper.insert(tbBrand);
    }

    @Override
    public TbBrand findOne(long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void dele(Long[] ids) {
        if (ids!=null){
            for (Long id:ids){
                tbBrandMapper.deleteByPrimaryKey(id);
            }
        }
    }

    @Override
    public List<Map> findBrandList() {
        return tbBrandMapper.findBrandList();
    }

}
